#pragma once

#include "Platform/PlatformInterface.h"


#include <thread>
#include <chrono>


int main(int argc, char* argv[])
{

  SSystemWindowCreateInfo CreateInfo;
  CreateInfo.Mode = ESystemWindowMode::Windowed_NormalSize;
  CreateInfo.Size = SIntPoint(800, 600);
  CreateInfo.Title = U"Hello World";

  auto Window= IPlatformInterface::GetPlatformInterface()->CreateSystemWindow(CreateInfo);

  //on Windows, window style is pop up, we need to paint it to make it visible
  /*
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

        EndPaint(hwnd, &ps);
    }
  
  */

  //test drag window
  int DragOffsetX=0;
  int DragOffsetY=0;
  bool bDraging=false;
  while(1)
  {
    //sleep 1ms
    std::this_thread::yield();

    auto EventOptional=IPlatformInterface::GetPlatformInterface()->PopEvents();

    if(!EventOptional.HasValue())
    {
      continue;
    }

    auto Event=EventOptional.GetValue();


    if(Event.IsA<SMouseButtonEvent>())
    {
      auto MouseEvent= Event.Get<SMouseButtonEvent>();

      if(MouseEvent.GetType()==EMouseEventType::ButtonDown)
      {
        if(MouseEvent.GetButton()==EMouseButton::Left)
        {
          DragOffsetX=MouseEvent.GetMosuePositionOnScreen().X-Window->GetWindowPosition().X;
          DragOffsetY=MouseEvent.GetMosuePositionOnScreen().Y-Window->GetWindowPosition().Y;
          bDraging=true;
        }
      }
      else if(MouseEvent.GetType()==EMouseEventType::ButtonUp)
      {
        if(MouseEvent.GetButton()==EMouseButton::Left)
        {
          DragOffsetX=0;
          DragOffsetY=0;
          bDraging=false;
        }
      }
      else if(MouseEvent.GetType()==EMouseEventType::Move)
      {
        if(bDraging)
        {
          Window->SetWindowPosition(MouseEvent.GetMosuePositionOnScreen()-SIntPoint(DragOffsetX,DragOffsetY));
        }
      }



    }
  
    //log when window resized
    if(Event.IsA<SWindowEvent>())
    {
      auto WindowEvent=Event.Get<SWindowEvent>();
      if(WindowEvent.GetType()==EWindowEventType::Resize)
      {
        std::cout<<"Window Resized"<<std::endl;
      }
    }
  
  
  }

}